package com.jeeplus.modules.guide.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.modules.guide.service.MatterBusinessService;
import com.jeeplus.modules.guide.service.MatterSelectService;
import com.jeeplus.modules.guide.service.VersionService;
import com.jeeplus.modules.guide.service.WarningService;
import com.jeeplus.modules.guide.utils.HttpQueueUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 窗口预警记录
 */
@RestController
@RequestMapping("/warning")
@RequiredArgsConstructor
public class WarningController {
    private final WarningService warningService;
    private final MatterBusinessService matterBusinessService;
    private final MatterSelectService matterSelectService;
    private final VersionService versionService;


    //排号语音接口
    @Value("${voice}")
    private String voice;
    //对接AppKeyValue
    @Value("${AppKeyValue}")
    private String AppKeyValue;


    @GetMapping(value ="warning")
    //@ApiOperation("预警")
    public AjaxJson warning() {
        String warning = warningService.warning();

        return AjaxJson.success("ok");
    }
    @GetMapping(value ="getBusiness")
    public AjaxJson getBusiness(String sysno ,String no) {
        List<Map<String, String>> business = matterBusinessService.getBusiness(sysno, no);
        if(business!=null){
            for (int i = 0;  i  < business.size(); i++) {
                for (int k = i+1; k < business.size(); k++) {
                    if(business.get(i).get("S_id")==business.get(k).get("S_id")){
                        business.remove(i);
                    }
                }

                }

            }
        return AjaxJson.success().put("data",business);
    }
    @GetMapping(value ="matterAndMatterId")
    public AjaxJson matterAndMatterId(String typeId,String areaId) {
        String cacheVersion = versionService.getVersion();
        List<Map<String, String>> matterAndMatterId = matterSelectService.getMatterAndMatterId(typeId, cacheVersion, areaId);
        return AjaxJson.success().put("data",matterAndMatterId);
    }

    @GetMapping(value ="getAll")
    public AjaxJson getAll(String areaId,String no) {
        List<Map<String, String>> matterAndMatterId =new ArrayList<>();
        String cacheVersion = versionService.getVersion();
        List<Map<String, String>> business = matterBusinessService.getBusiness(areaId, no);
        for (Map<String, String> map : business) {
            List<Map<String, String>> s_id = matterSelectService.getMatterAndMatterId(map.get("S_id"), cacheVersion, areaId);
            for (Map<String, String> stringStringMap : s_id) {
                stringStringMap.put("typeName",map.get("S_typename"));
            }
//            if(){
//
//            }
            matterAndMatterId.addAll(s_id);
        }
        return AjaxJson.success().put("data",matterAndMatterId);
    }
    @GetMapping(value ="setVoice")
    public AjaxJson setVoice(@RequestHeader("AppKey") String AppKey,String context) {
        if (AppKeyValue.equals(AppKey)) {
            return AjaxJson.success("get data success!").put("data", HttpQueueUtils.setVoice(voice, context));
        } else {
            return AjaxJson.error("AppKey is error!").put("code", 400);
        }
    }

}
